package com.yikes.yygh.web.exception;

import com.yikes.yygh.common.enums.ResultCodeInterface;
import lombok.Getter;

/**
 * <p>
 * 自定义业务异常
 * </p>
 *
 * @Author: guanrong.yin
 * @Date: 2024/12/1 21:57
 */
@Getter
public class BizException extends RuntimeException {

    public ResultCodeInterface codeInterface;

    public BizException(ResultCodeInterface errorCode) {
        super(errorCode.getMsg());
        this.codeInterface = errorCode;
    }

    public BizException(String message) {
        super(message);
    }

    public BizException(String message, Throwable cause) {
        super(message, cause);
    }

    public BizException(Throwable cause) {
        super(cause);
    }


//    public static Exception build(InfoResultCode commonEnum) {
//
//        return new BizException(commonEnum);
//    }

    /**
     * 自定义异常描述
     *
     * @param commonEnum 异常枚举类
     * @param obj        描述信息
     */
    public static BizException build(ResultCodeInterface commonEnum, Object... obj) {

        ResultCodeInterface factory = new ResultCodeInterface() {

            @Override
            public String getCode() {
                return commonEnum.getCode();
            }

            @Override
            public String getMsg() {
                return (obj == null || obj.length == 0) ? commonEnum.getMsg() : String.format(commonEnum.getMsg(), obj);
            }
        };
        return new BizException(factory);
    }

    public static BizException build(String code, String msg) {

        ResultCodeInterface factory = new ResultCodeInterface() {

            @Override
            public String getCode() {
                return code;
            }

            @Override
            public String getMsg() {
                return msg;
            }
        };

        return new BizException(factory);
    }

}
